//
//  SQLookPlanViewController.m
//  energy
//
//  Created by 苏青 on 2021/9/30.
//

#import "SQLookPlanViewController.h"
#import "SQLookPlanModel.h"
#import "WorkDetailViewController.h"
#import "SVProgressHUD.h"
@interface SQLookPlanViewController ()<BaseTableVCInterface>


@end

@implementation SQLookPlanViewController

- (void)viewDidLoad {
    self.tableViewInterface = self;
    [super viewDidLoad];
    self.navigationItem.title = @"应急响应预案";
    [self getLookPlanData];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getLookPlanData];
    }];
    LC_WEAK_SELF
    self.cellEvent = ^(int key) {
        SQLookPlanModelFiles * dataModel = weakSelf.dataSouece[key];
        [SVProgressHUD show];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            // 获取Caches目录
            NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

            NSString * filePath = [cachesPath stringByAppendingFormat:@"/%@",dataModel.epd_name];
            NSFileManager *fileManager = [NSFileManager defaultManager];
            if ([fileManager fileExistsAtPath:filePath]) {

            }else{
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",LC_FilesHeader,dataModel.epd_enclosure]];
            NSData *data = [NSData dataWithContentsOfURL:url];
            NSString * filePath = filePath = [weakSelf addFile:dataModel.epd_name contents:data];
            }
            
            if (filePath.length != 0) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                     WorkDetailViewController *vc = WorkDetailViewController.new;
                    vc.filePath = filePath;
                    vc.n_title = dataModel.epd_grade_text;
                    [weakSelf pushVC:vc];
                });
            }
            
        });
    };
}
-(void)getLookPlanData{
    [self.dataSouece removeAllObjects];
    [SQLookPlanModel getDataWithParams:@{@"erp_enterprise":MEnterpriseId,@"erp_er":self.erp_er,@"er_grade_id":self.er_grade_id} WithCallBack:^(SQLookPlanModel * data) {
     
        for (int i = 0; i < data.data.count; i ++) {
            SQLookPlanModelData * dataModel = data.data[i];
            for (SQLookPlanModelFiles * filesModel in dataModel.files) {//
                [self.dataSouece addObject:filesModel];
            }
            
        }
        
        [self.tableView.mj_header endRefreshing];
        [self reloadTableView];
        
    }];
}

-(BaseModel *)modelForCell:(NSIndexPath *)indexPath{
    return self.dataSouece[indexPath.row];
}



-(NSInteger)rowsCount:(NSInteger)section{
    return self.dataSouece.count;
}


- (NSArray<NSArray<NSString *> *> *)classArr{
    return @[@[@"SQLookPlanTableViewCell"]];
}

- (NSString *)addFile:(NSString *)filePath contents:(NSData *)data{
    // 获取Caches目录
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    filePath = [cachesPath stringByAppendingFormat:@"/%@",filePath];
    
    if (filePath.length==0) {
        return @"";
    }
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        return filePath;
    }
    NSError *error;
    NSString *dirPath = [filePath stringByDeletingLastPathComponent];
    BOOL isSuccess = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
    if (error) {
        NSLog(@"creat File Failed:%@",[error localizedDescription]);
    }
    if (!isSuccess) {
        return @"";
    }
    isSuccess = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
    
    [data writeToFile:filePath atomically:YES];
    return filePath;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
